package com.augurit.common.sysmeta.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.base.extmodel.ExtResult;
import com.augurit.common.sysmeta.service.ISysMetaColumnService;
import com.augurit.common.sysmeta.web.form.SysMetaColumnForm;
import com.augurit.common.system.util.WebContextUtils;
import com.augurit.common.util.ExtUtils;
import com.augurit.wf.bi.web.form.WfActElemPrivDefForm;

/**
 * 字段结构元数据定义Action类
 *
 */
public class SysMetaColumnAction extends ExtCrudActionSupport<SysMetaColumnForm>{

	private static final long serialVersionUID = 6684539436271966958L;

	@Autowired 
	private ISysMetaColumnService sysMetaColumnService;
	
	private Long id;
	private Long[] ids;
	private Long tableId;
	private SysMetaColumnForm form;
	private String entityNameStrs;
	private Page<SysMetaColumnForm> page = new Page<SysMetaColumnForm>(15);
	private String masterEntity;
	private String queryProperties;
	@Override
	public String delete() throws Exception {
		if(id != null){
			sysMetaColumnService.delete(id);
			this.extRenderSuccess();
		}
		return null;
	}

	public String deleteMore() throws Exception {
		if(ids != null){
			sysMetaColumnService.delete(ids);
			this.extRenderSuccess();
		}
		return null;
	}

	public String getColumnsByTableId(){
		if(tableId != null){
			ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
			page = sysMetaColumnService.getColumnsByTableId(
					tableId,page,WebContextUtils.getSysCodeBuffer(this.getApplication()));
			this.extRenderGridJson(page);
		}
		return null;
	}
	
	public String getColumnsGroupByEntityNames() throws Exception{
		if(this.entityNameStrs != null && entityNameStrs.trim().length()>0){
			String entityNames[]= convertStringToArray(entityNameStrs);
			List<SysMetaColumnForm> result = sysMetaColumnService.getColumnsGroupByTableNames(form, entityNames);
			if(result != null && result.size() > 0)
				this.extRenderGridJson(result);
			else{
				ExtResult extResult = new ExtResult();
				extResult.setTotalCount(0);
				extResult.setResult(new ArrayList<WfActElemPrivDefForm>(1));
				extResult.setSuccess(true);
				ExtBaseAction.renderJson(extResult);
			}
				
		}
		return null;
	}
	
	public String getKeyColumnsGroupByEntityNames() throws Exception{
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		if(this.entityNameStrs != null && entityNameStrs.trim().length()>0){
			String entityNames[]= convertStringToArray(entityNameStrs);
			page = sysMetaColumnService.getColumnsGroupByTableNames(page,entityNames," and ps.isPrimaryKey='Y'");
			this.extRenderGridJson(page);
		}
		return null;
	}
	
	public String getColumnsByPropertyNames() throws Exception{
		if(masterEntity != null && masterEntity.trim().length()>0 
			&& queryProperties != null && queryProperties.trim().length()>0){
			List<SysMetaColumnForm> list = sysMetaColumnService.getColumnsByPropertyNames(masterEntity, queryProperties);
			
			if(list != null && list.size()>0){
				String jsonString = Struts2Utils.getJsonString(list);
				SysMetaColumnAction.renderText(jsonString);
			}
		}
		return null;
	}
	
	public String input()throws Exception {
		if(form != null){
			this.extRenderFormJson(form);
		}
		return null;
	}
	
	/**
	 * 解析"Notice bus,OmOrg omOrg"
	 * @return
	 */
	public String[] convertStringToArray(String entityNameStrs){
		if(entityNameStrs != null && entityNameStrs.trim().length()>0){
			String strs[]= entityNameStrs.split(",");
			String entityNames[] = new String[strs.length];
			for(int i=0;i<strs.length;i++){
				String entityName = strs[i].split(" ")[0];//解析"Notice bus"
				entityNames[i] = entityName;
			}
			return entityNames;
		}
		return null;
	}
	
	@Override
	public String list() throws Exception {
		if(form != null){
			ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
			page = sysMetaColumnService.search(
					form,page,WebContextUtils.getSysCodeBuffer(this.getApplication()));
			this.extRenderGridJson(page);
		}
		return null;
	}
	
	public SysMetaColumnForm getModel() {
		return form;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(id != null)
			form = sysMetaColumnService.get(id);
		else
			form = new SysMetaColumnForm();
	}

	@Override
	public String save() throws Exception {
		if(form != null){
			sysMetaColumnService.save(form);
			this.extRenderSuccess();
		}
		return null;
	}
	
	
	//属性的getter和setter方法
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public Long getTableId() {
		return tableId;
	}

	public void setTableId(Long tableId) {
		this.tableId = tableId;
	}

	public SysMetaColumnForm getForm() {
		return form;
	}

	public void setForm(SysMetaColumnForm form) {
		this.form = form;
	}

	public String getEntityNameStrs() {
		return entityNameStrs;
	}

	public void setEntityNameStrs(String entityNameStrs) {
		this.entityNameStrs = entityNameStrs;
	}

	public String getMasterEntity() {
		return masterEntity;
	}

	public void setMasterEntity(String masterEntity) {
		this.masterEntity = masterEntity;
	}

	public String getQueryProperties() {
		return queryProperties;
	}

	public void setQueryProperties(String queryProperties) {
		this.queryProperties = queryProperties;
	}

	
}
